Defining an API Slice with createApi() in RTK Query
In RTK Query, an API slice is defined using the createApi() function. This slice manages how your app interacts with APIs — including fetching, caching, and updating server data — directly within the Redux store.
reducerPath: A unique key to identify the API slice in the Redux store.
baseQuery: Defines how API requests are made, often using fetchBaseQuery.
endpoints: A function that declares all queries and mutations (data fetching and modifying endpoints).
The createApi() function automatically generates hooks like useGetUsersQuery and useAddUserMutation, making it easy to integrate API data directly into React components.
By defining an API slice this way, you centralize API logic, reduce boilerplate, and enable features like caching, invalidation, and automatic refetching out of the box.